$(document).ready(function () {
    setupData();
    promptBox();

})
$(document).mouseup(function(e){
    var infoType;
    var infoData;
    var checkBoxInput = $("input[name='StaffCheckboxName']:checkbox");
    var child = $("#staffInfo-list");   // 设置目标区域
    var childBtn = $(".CutStaff");
    if(!child.is(e.target) && child.has(e.target).length === 0){ //表格外
        if(!childBtn.is(e.target) && childBtn.has(e.target).length === 0){// （表格和）按钮外
            checkBoxInput.removeAttr('checked');
            $(checkBoxInput).parent().removeClass('JudgeIconSelected');
        }else{ //按钮
            cutStaff(infoType,infoData);
        }
    }else{ //表格
    }
})
var context = {
    infoList: []
};
function setupData() { // LeanCloud - 查询
    var query = new AV.Query('StaffList');
    query.find().then(function (infoList) {
        infoList.forEach(function(info) {
            var name = info.get('dataName');
            var sex = info.get('dataSex');
            var phone = info.get('dataPhone');
            var id = info.get('dataId');
            var part = info.get('dataPart');
            var job = info.get('dataJob');
            var door = info.get('dataDoor');
            var type = info.get('dataType');
            var objectId = info.get('objectId')
            var infoArray = {
                "name":name, "sex":sex,
                "phone":phone, "id":id,
                "part":part, "job":job,
                "door":door,"type":type,"objectId":objectId
            };
            context.infoList.push(
                infoArray
            )
        });
        $('#staffList').tmpl(context).appendTo('#staffInfo-list');
        select();
    }).catch(function(error) {
        alert(JSON.stringify(error));
    });
}
function select() {
    $($(".Info-row").parent().children()).each(function () {
        var checkBox = $(this).find(".StaffCheckbox");
        $(this).mouseover(function () {
            $(this).addClass('TdSelect');
            $(this).click(function () { //行点击
                // console.log("--------")
                $(checkBox).parent().parent().siblings().find(".JudgeIcon").removeClass('JudgeIconSelected')
                $("input[name='StaffCheckboxName']:checkbox").removeAttr('checked');
                $(checkBox).attr('checked', true);
                $(this).find(".JudgeIcon").addClass('JudgeIconSelected');
            });
            $(this).find(".JudgeIcon").addClass('JudgeIconSelect');
        });
        $(this).mouseout(function () {
            $(this).removeClass('TdSelect')
            $(this).find(".JudgeIcon").removeClass('JudgeIconSelect');
        })
    })
}
function cutStaff(infoType,infoData) {

    var cutArray = [];
    $($(".Info-row").parent().children()).each(function () {
        var checkBox = $(this).find(".StaffCheckbox");
        cutArray.push(checkBox)
    })
    for(var i=0;i<cutArray.length;i++){
        if($(cutArray[i]).is(':checked')){
            console.log($(cutArray[i]).parent())
            infoData = $(cutArray[i]).parent().prev().text();
            infoType = true;
            break; //each无法中断--for
        }else{
            infoType = false;
        }
    }
    judgeStatus(infoType,infoData) //判断删除
}
function judgeStatus(infoType,infoData) {
    if(infoType == true){
        $(".CutStaff").attr('data-target','#cutInfoModal');
        $(".confirm-cut-btn").click(function () {  //弹框里点击确定
            var StaffList = AV.Object.createWithoutData('StaffList', infoData);
            StaffList.destroy().then(function (success) { //删除成功
                window.location.href = "../../../company/view/staffInfo.html"
            }, function (error) { // 删除失败
                alert(JSON.stringify(error));
            });
        })
    }else{
        $(".CutStaff").attr('data-target',null);//禁止模态框
        $("input[id='prompt-show-font']").val("尚未选中学生，无法删除");
        $(".alert-prompt-box").removeClass('hide');
        $("#prompt-box").removeClass('hide');
    }
}
function promptBox() {
    $(".button-ok").click(function () {
        $("#prompt-box").addClass('hide')
        $(".alert-prompt-box").addClass('hide')
    })
}


